package com.golxen.framework.activity.dao;


import com.golxen.framework.activity.dao.entity.ActivityInfo;
import com.golxen.framework.activity.eActivityType;
import com.golxen.framework.db.DBManager;
import com.golxen.framework.db.dao.AbstractDaoImpl;
import com.golxen.framework.event.condition.dao.entity.ActivityConditionInfo;
import com.golxen.utils.StringUtils;
import com.golxen.utils.TimeUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 活动
 */
public class ActivityDaoImpl extends AbstractDaoImpl {


	public Map<Integer, ActivityInfo> getAllRunningActivityInfoMap(){
		ResultSet rs = null;
		CallableStatement call = null;
		Map<Integer, ActivityInfo> dataMap = new ConcurrentHashMap<>();
		String sql = "select * from t_s_activity_main where beginShowTime <= ? AND ? <= endShowTime";
		try {
			getDb().setConnName(DBManager.serverDbName);
			call = getDb().getConnection().prepareCall(sql);
			call.setString(1, TimeUtils.getCurrentDateTimeString() );
			call.setString(2,TimeUtils.getCurrentDateTimeString() );
			rs = call.executeQuery();
			while (rs.next()) {
				ActivityInfo data = initActivityInfo(rs);
				dataMap.put(data.getActivityId(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	//通过activityId获取数据库的单个活动
	public ActivityInfo getActivityInfoByActivityId(int activityId){
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_s_activity_main where activityId = ?";
		try {
			getDb().setConnName(DBManager.serverDbName);
			call = getDb().getConnection().prepareCall(sql);
			call.setInt(1, activityId);
			rs = call.executeQuery();
			while (rs.next()) {
				ActivityInfo data = initActivityInfo(rs);
				return data;
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return null;
	}

	public Map<Integer, ActivityInfo> getActivityInfoMap(long serverId) {
		ResultSet rs = null;
		CallableStatement call = null;
		Map<Integer, ActivityInfo> dataMap = new ConcurrentHashMap<>();
		String sql = "select * from t_s_activity_main where serverList LIKE ?";
		try {
			getDb().setConnName(DBManager.serverDbName);
			call = getDb().getConnection().prepareCall(sql);
			call.setString(1, "%" + serverId + "%");
			rs = call.executeQuery();
			while (rs.next()) {
				ActivityInfo data = initActivityInfo(rs);
				dataMap.put(data.getActivityId(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}



	private ActivityInfo initActivityInfo(ResultSet rs) throws SQLException {
		ActivityInfo data = new ActivityInfo();
		data.setActivityId(rs.getInt("activityId"));
		data.setActivityName(rs.getString("activityName"));
		data.setActivityDesc(rs.getString("activityDesc"));
		data.setType(rs.getInt("type"));
		data.setBeginShowTime(rs.getTimestamp("beginShowTime").getTime() / 1000);
		data.setEndShowTime(rs.getTimestamp("endShowTime").getTime() / 1000);
		data.setBeginTime(rs.getTimestamp("beginTime").getTime() / 1000);
		data.setEndTime(rs.getTimestamp("endTime").getTime() / 1000);
		data.setParam1(rs.getString("param1"));
		data.setParam2(rs.getString("param2"));
		data.setParam3(rs.getString("param3"));
		data.setParam4(rs.getString("param4"));
		data.setParam5(rs.getString("param5"));
		data.setParam6(rs.getString("param6"));
		data.setServerListStr(rs.getString("serverList"));
		data.setServerIdList(StringUtils.stringToLongList(data.getServerListStr(), ","));
		data.setCrossId(rs.getLong("crossServerId"));
		return data;
	}

	public void getActivityConditionInfoMap(Map<Integer, Map<Integer, List<ActivityConditionInfo>>> activityConditionInfoTypeMap,
			Map<Integer, List<ActivityConditionInfo>> activityConditionInfoMap, List<Integer> activityIdList) {
		ResultSet rs = null;
		CallableStatement call = null;
		if(null != activityIdList && activityIdList.size() > 0) {
			String sql = "select * from t_s_activity_condition where activityId in(" + StringUtils.listToString(activityIdList, ",") + ")";
			try {
				getDb().setConnName(DBManager.serverDbName);
				call = getDb().getConnection().prepareCall(sql);
				rs = call.executeQuery();
				while (rs.next()) {
					ActivityConditionInfo data = initActivityConditionInfo(rs);

					if (!activityConditionInfoMap.containsKey(data.getActivityId())) {
						activityConditionInfoMap.put(data.getActivityId(), new ArrayList<>());
					}
					activityConditionInfoMap.get(data.getActivityId()).add(data);
					if (!activityConditionInfoTypeMap.containsKey(data.getActivityId())) {
						activityConditionInfoTypeMap.put(data.getActivityId(), new ConcurrentHashMap<>());
					}
					Map<Integer, List<ActivityConditionInfo>> dataMap = activityConditionInfoTypeMap.get(data.getActivityId());
					if (!dataMap.containsKey(data.getType())) {
						dataMap.put(data.getType(), new ArrayList<>());
					}
					dataMap.get(data.getType()).add(data);
				}
			} catch (Exception e) {
				throw new RuntimeException(e);
			} finally {
				getDb().closeConnection(call, rs);
			}
		}
	}


	private ActivityConditionInfo initActivityConditionInfo(ResultSet rs) throws SQLException {
		ActivityConditionInfo data = new ActivityConditionInfo();
		data.setConditionId(rs.getInt("id"));
		data.setActivityId(rs.getInt("activityId"));
		data.setDesc(rs.getString("desc"));
		data.setType(rs.getInt("type"));
		data.setParamStr(rs.getString("params"));
		data.setParamList(StringUtils.stringToBigIntegerList(data.getParamStr(), "\\|"));
		String value = rs.getString("extendParam");
		if(!StringUtils.isNullOrEmpty(value)){
			data.setExtendParam(value);
		}
		return data;
	}



	public Map<Integer, ActivityInfo> getOpenActivityInfoMap(eActivityType activityType, String nowTimeStr, long crossServerId) {
		ResultSet rs = null;
		CallableStatement call = null;
		Map<Integer, ActivityInfo> dataMap = new ConcurrentHashMap<>();
		String sql = "select * from t_s_activity_main where type = " + activityType.getValue() + " and  endShowTime>=? and crossServerId = ?";
		try {
			getDb().setConnName(DBManager.serverDbName);
			call = getDb().getConnection().prepareCall(sql);
			call.setString(1, nowTimeStr);
			call.setLong(2, crossServerId);
			rs = call.executeQuery();
			while (rs.next()) {
				ActivityInfo data = initActivityInfo(rs);
				dataMap.put(data.getActivityId(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	public Map<Integer, ActivityInfo> getActivityInfoMapByType(eActivityType activityType) {
		ResultSet rs = null;
		CallableStatement call = null;
		Map<Integer, ActivityInfo> dataMap = new ConcurrentHashMap<>();
		String sql = "select * from t_s_activity_main where beginShowTime < ? and endShowTime > ? and `type`=? order by activityId";
		try {
			getDb().setConnName(DBManager.serverDbName);
			call = getDb().getConnection().prepareCall(sql);
			int i = 1;
			call.setString(i++, TimeUtils.getCurrentDateTimeString());
			call.setString(i++, TimeUtils.getCurrentDateTimeString());
			call.setInt(i++, activityType.getValue());
			rs = call.executeQuery();
			while (rs.next()) {
				ActivityInfo data = initActivityInfo(rs);
				dataMap.put(data.getActivityId(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	public Map<Integer, ActivityInfo> getOpenActivityInfoMap(eActivityType activityType, String nowTimeStr) {
		ResultSet rs = null;
		CallableStatement call = null;
		Map<Integer, ActivityInfo> dataMap = new ConcurrentHashMap<>();
		String sql = "select * from t_s_activity_main where type = " + activityType.getValue() + " and endShowTime>=? order by activityId";
		try {
			getDb().setConnName(DBManager.serverDbName);
			call = getDb().getConnection().prepareCall(sql);
			call.setString(1, nowTimeStr);
			rs = call.executeQuery();
			while (rs.next()) {
				ActivityInfo data = initActivityInfo(rs);
				dataMap.put(data.getActivityId(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}


}
